﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

public class Record
{
    /**服务器表格名*/
    private string _recName = "";

    /**表格二维数组*/
    private ArrayList _recArr;

    /**用于记录表格每列的数据类型*/
    private List<int> _nColType = new List<int>();

    /**
     * 创建表格数据
     * @param val	表格名
     * 
     */
    public Record(string val)
    {
        _recName = val;
    }

    /**表格二维数组*/
    public ArrayList recArr
    {
        get { return (ArrayList)_recArr.Clone(); }
        set { _recArr = value; }
    }

    /**服务器表格名*/
    public string recName
    {
        get { return _recName; }
        set { _recName = value; }
    }

    /**
     * 设置表格结构 
     * @param nColType	：记录表格每列的信息
     * 
     */
    public void SetColType( List<int> nColType)
    {
        _nColType = nColType;
    }

    /**
     * 取表格列数 
     * @return 
     * 
     */
    public int GetCols()
    {
        return _nColType.Count;
    }

    /**
     * 插入数据
     * @param row	：插入到row行
     * @param count	：插入count条数据
     * @param msg	：数据
     * 
     */
    public void AddRow(int rowIndex, int count, ByteArrayEx msg)
    {
        //表格列数
        int tColLength = GetCols();

        //表格列数据类型
        uint tColType;

        //待插入的数组
        ArrayList tDataArr = new ArrayList();

        //待插入的一行数据
        ArrayList tColDataArr;

        for (int i = 0; i < count; i++)
        {
            tColDataArr = new ArrayList();
            for (int j = 0; j < tColLength; j++)
            {
                tColType = (uint)_nColType[j];
                tColDataArr.Insert(j, msg.GetDataByType(tColType));
            }
            tDataArr.Insert(tDataArr.Count, tColDataArr);
        }

        int tIndex;
        //派发绑定
        for (int k = 0; k < count; k++)
        {
            tIndex = rowIndex + k;

            //将数据插入到本地数据缓存中
            _recArr.Insert(tIndex, tDataArr[k]);

            BindManage.getInstance().CallRecord(this, tIndex, _recArr[tIndex], true);
        }
    }

    /**
     * 修改表格单元格， 
     * @param nCount
     * @param msg	（包含行列及修改值）
     * 
     */
    public void ChangeRecordGrid(uint nCount, ByteArrayEx msg)
    {
        //表格列数据类型
        uint tColType;

        for (int i = 0; i < nCount; i++)
        {
            int nRow = msg.readUnsignedShort();
            int nCol = msg.readByte();

            if (_recArr[nRow] == null)
            {
                _recArr[nRow] = new ArrayList();
            }

            tColType = (uint)_nColType[nCol];

            (_recArr[nRow] as ArrayList)[nCol] = msg.GetDataByType(tColType);//Insert(nCol,msg.GetDataByType(tColType));

            //派发绑定
            BindManage.getInstance().CallRecord(this, nRow, _recArr[nRow], true);
        }
    }

    /**
     * 删除表格某一列 
     * @param rowIndex
     */
    public void DelRow(int rowIndex)
    {
        ArrayList arr = (ArrayList)_recArr[rowIndex];
        _recArr.RemoveAt(rowIndex);
        //先删除行
        BindManage.getInstance().CallRecord(this, rowIndex, arr[0], false);
    }

    /**
     * 清空表格
     */
    public void RecordClear()
    {
        int tSize = _recArr.Count;
        ArrayList tempArray = _recArr;
        _recArr = new ArrayList();
        //先删除行
        for (int j = 0; j < tSize; j++)
        {
            //DelRow(j);
            BindManage.getInstance().CallRecord(this, j, tempArray[j], false);
        }
    }

}
